//
//  YZGAlertView.m
//  Masonry
//
//  Created by LXY on 16/6/14.
//  Copyright © 2016年 AVGD. All rights reserved.
//

#import "MJUIAlertBaseView.h"
@interface MJUIAlertBaseView()<UIGestureRecognizerDelegate>


@end

@implementation MJUIAlertBaseView



#pragma mark **************** init
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissWindow)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];//添加手势

    }
    return self;
}

#pragma mark ************** 显示
- (void)show {
    if(self.alertType == AlertTypeCenter){
        [UIView animateWithDuration:0.3f animations:^{
            [self setBackgroundColor:UIColor.mj_windowColor];
            self.contentview.alpha = 1;

        } completion:nil];
    }else{
        [UIView animateWithDuration:0.3f animations:^{
            [self setBackgroundColor:UIColor.mj_windowColor];
            self.contentview.frame = CGRectMake(0, SCREEN_HEIGHT-self.contentHeight, SCREEN_WIDTH, self.contentHeight);

        } completion:nil];
    }
    
}
#pragma mark ************** 消失
- (void)dismissWindow
{
    [self dismiss];
}
- (void)dismiss
{
    
    if(self.alertType == AlertTypeCenter){
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.alpha = 0;

        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }else{
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, self.contentHeight);

        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    
}



#pragma mark ************** 父控件布局完成
- (void)layoutSubviews
{
    [super layoutSubviews];
}
#pragma mark ************** 子视图不响应父视图手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:self.contentview]) {
        return NO;
    }
    
    return YES;
    
}

@end
